package pro.spring.controller.frontController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pro.spring.model.Users;

@Controller
@RequestMapping("login")
public class LoginController extends BaseController{
	
	private String local = "page/front/login/";
	
	@RequestMapping(value="login.html",method=RequestMethod.GET)
	public String index(HttpServletRequest request,
			HttpServletResponse response){
		Integer type = switchService.selectType();
		HttpSession session=request.getSession();
		session.setAttribute("switch",type);
		return local+"login";
	}
	
	@RequestMapping(value="logi.html",method=RequestMethod.GET)
	public String index(){
		return "redirect:login/login.html";
	}
	
	@RequestMapping(value="out.html",method=RequestMethod.GET)
	public String out(HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session=request.getSession();
		session.setAttribute("uId",null);
		return local+"login";
	}
	
	@ResponseBody
	@RequestMapping(value="log.json",method=RequestMethod.POST)
	public Integer login(HttpServletRequest request,
			HttpServletResponse response,Users user){
		Integer num = 0;
		HttpSession session=request.getSession();
		Users u = usersService.selectLogin(user.getuAccount(),user.getuPassword());
		if(u != null){
			num = 1;
			session.setAttribute("uName",u.getuName());
			session.setAttribute("uId",u.getuId());
		}
		return num ;
	}

	@ResponseBody
	@RequestMapping(value="check.json",method=RequestMethod.POST)
	public Integer cheng(HttpServletRequest request,
			HttpServletResponse response,Users user){
			int num = 0 ;
			if(usersService.selectByEmail(user.getuEmail())!=null){
				num = 2;
				System.out.println("邮箱已存在");
			}
			if(usersService.selectByAccount(user.getuAccount())>0){
				num = 3;
				System.out.println("账号已存在");
			}
			if(num==0){
				num = usersService.insert(user);
			}
		return num;
	}
}
